package galaxy.app.network.rpc.client;

import java.util.HashMap;
import java.util.Map;

import galaxy.app.log.LogHelper;
import galaxy.kernel.annotation.Route;
import galaxy.kernel.component.AbstractECSComponent;
import galaxy.kernel.utils.RandomUtils;

public class RpcClientComponent extends AbstractECSComponent {
    public Map<Class<?>, Route> rpcInfoMap = new HashMap<>();

    /**
     * key:nodeType, value:{key:nodeId,value:NettyRpcClient}
     */
    public Map<String, Map<String, NettyRpcClient>> clientMaps = new HashMap<>();

    public Map<String, SelectClientFactory> clientRouterMap = new HashMap<>();

    /**
     * random获取一个RpcClient
     */
    SelectClientFactory defaultClientRoute = (session, nodeType, request) -> {
        Map<String, NettyRpcClient> clients = clientMaps.get(nodeType);
        if (clients == null) {
            LogHelper.warn("can not find remote client for nodeType = {}", nodeType);
            return null;
        }
        final String index = RandomUtils.randomHit(clients.keySet());
        return clients.get(index);
    };

    public String[] nodeTypes;
}
